<?php
include_once "core/Database.php";
include_once "core/Task.php";

$sqlite = "paxgui_content.sqlite";
//$sqlite = "AVA_qtr_20130925_161218.sqlite";


$obj = new Database($sqlite);


$tasks = json_decode(file_get_contents('templates/main.json'), true);

$obj = new Task($sqlite, 25);

$data = array();

$GENRE_LIST = array();
$CONTENT_LIST = array();
$GENRE_LEAF = array();

foreach ($tasks as $type => $task) {
	$GENRE_LEAF[$type] = array();
	if ($type  == 'vod')
	start($task, $type);
}

//print_r($GENRE_LEAF);
print_r($GENRE_LIST);
//buildGenreTree();
//buildContent();
//print_r($CONTENT_LIST);

function start($task, $type) {
	global $GENRE_LIST, $CONTENT_LIST, $obj;

	$genreList = $obj->validateGenre($type . '_genre', -1);
	$GENRE_LIST[$type] = genreTraversal($genreList, $type, -1);
}

function buildContentList($task, $type) {
	global $obj, $GENRE_LIST, $CONTENT_LIST;

	if ($task['by_genre']) {
		$data = $GENRE_LIST[$type]['result'];

		foreach ($data as $genreId => $val) {
			$isLeaf = intval($val['is_leaf']);

			$parentId = intval($val['parent_id']['value']);

			if (-1 == $parentId) {


			}
			$tmp = $obj->validateContent($type, $genreId);
			$data = array('genre' => $val['description']['value'], 'content_list' => $tmp['result']);
			
			$CONTENT_LIST[$type][$genreId] = $data;
		}
	}
	else { // all
	}

	if ($task['kid']) {
	}
}

/**
 * traverse the genre tree.
 */

function genreTraversal($genreList, $type, $parentId = -1) {
	global $obj, $GENRE_LEAF;
	$result = array();

	foreach($genreList['result'] as $genreId => $val) {
		$is_leaf = intval($val['is_leaf']['value']);

		if (-1 == $is_leaf) { // not a leaf, keep going
			$tmp = $obj->validateGenre($type . '_genre', $genreId);
			$data = genreTraversal($tmp, $type, $genreId);
			$ret = array('root' => $val);

//			if (isset($data['result'])) {
//				$ret['root']['branch'] = $data['result'];
//			}

			$ret['root']['branch'] = genreTraversal($data, $type, $genreId);

			$result[$genreId] = $ret;
		}
		else {
			return $genreList;
		}
	}

	return $result;
}

function buildContentx() {
	global $CONTENT_LIST;

	echo '<div id="contentPanel">';

	foreach ($CONTENT_LIST as $type => $lists) {
		echo '<h2>' . $type . '</h2>';
		foreach ($lists as $genreId => $contentList) {
			echo '<div class="contentHeader">';
			echo 'There are ' . count($contentList['content_list']) . ' title(s) for this genre.';
			echo '<div id="aodNext" onclick="nextContent(\'' . $type . $genreId . '\');">Next</div>';
			echo '</div><br />';
			echo '<div>';

			$cmsIdList = array();
			foreach ($contentList['content_list'] as $t) {
				foreach ($t as $cms_id => $content) {
					$cmsIdList[] = $cms_id;
					echo "\n";
					echo '<div class="contentAtom" id="' . $cms_id . '">';
					foreach ($content as $key => $value) {
						echo $key . ':' . $value['value'] . "<br />\n";
					}

					echo '</div>';
					echo "<br />\n";
					echo '<script language="javascript">';
					echo 'var '. $type . ' = ' . json_encode($cmsIdList) . ';';
					echo '</script>';
				}
			}

			echo '</div>';
		}
	}

	echo '</div>';
}

function buildGenreTree() {
	global $GENRE_LIST;

	echo '<div id="genreList">';

	foreach ($GENRE_LIST as $type => $list) {
		echo "<h1>{$type}</h1>";

		echo "<ul>";

		foreach ($list as $genreId => $val) {
			echo "<li onclick='showContent({$genreId});'>" . $val['description']['value'] . "</li>";
		}

		echo "</ul>";
	}
	echo '</div>';
}
?>

</body>
</html>
